require("utils")
require("tier2")
require("gems") -- gems.lua
require("enchants") -- enchants.lua
require("eq") -- eq.lua
require("buffs") -- buffs.lua
require("races") -- races.lua

local warrior = {
	race = "HUMAN",
	class = "WARRIOR",
	level = 70,
	can_parry = true,
	can_block = true,
	innate = {
		strength = 144,
		agility = 96,
		stamina = 132,
		intellect = 33,
		spirit = 51,
		skill_defense = 350,
		maxhealth = 4440,
		parry = 5,
		block = 5,
	},
	auras = {
		{ type = "modifier", attribute = "spirit", value = 10 }, 
		{ type = "stat", attribute = "parry", value = 5 },
		{ type = "stat", attribute = "skill_defense", value = 20 },
		{ type = "stat", attribute = "block", value = 5 },
		{ type = "modifier", attribute = "base_armor", value = 10 }, 
		{ type = "stat", attribute = "expertise", value = 6 },
		{ type = "modifier", attribute = "blockvalue", value = 30 },
		{ type = "modifier", attribute = "stamina", value = 5 }, 
		{ type = "modifier", attribute = "strength", value = 10 }, 
	},
}

local eq = {
	head = {
		item = eq.head.w_t4,
		enchant = enchants.head.def_cot,
		socket1 = gems.meta.def,
		socket2 = gems.green.def_stam,
	},
	neck = {
		item = eq.neck.w_maiden,
	},
	shoulders = {
		item = eq.shoulders.w_d3,
		socket1 = gems.green.def_stam,
		socket2 = gems.blue.stam12,
		enchant = enchants.shoulders.def_scryer_honored,
	},
	back = {
		item = eq.back.w_badge,
	},
	chest = {
		item = eq.chest.w_d3,
		socket1 = gems.purple.agi_stam,
		socket2 = gems.purple.agi_stam,
		socket3 = gems.blue.stam12,
		enchant = enchants.chest.stam8,
	},
	wrists = {
		item = eq.wrists.w_h_kalithresh,
		enchant = enchants.wrists.stam12,
	},
	mainhand = {
		item = eq.mainhand.w_h_omor,
	},
	offhand = {
		item = eq.offhand.w_sh_badge,
	},
	ranged = {
		item = eq.ranged.w_h_gun,
	},
	hands = {
		item = eq.hands.w_d3,
		socket1 = gems.red.agility,
		socket2 = gems.green.def_stam,
		enchant = enchants.hands.stam10,
	},
	waist = {
		item = eq.waist.w_badge,
		socket1 = gems.blue.stam12,
		socket2 = gems.yellow.hit8,
	},
--~ 	legs = {
--~ 		item = eq.legs.w_kargath,
--~ 		socket1 = gems.orange.dodge_hit,
--~ 		socket2 = gems.yellow.def4,
--~ 		socket3 = gems.purple.str_stam,
--~ 		enchant = enchants.legs.stam_agi,
--~ 	},
	legs = {
		item = eq.legs.w_badge,
		socket1 = gems.purple.agi_stam,
		socket2 = gems.green.def_stam,
		socket3 = gems.blue.stam12,
		enchant = enchants.legs.stam_agi_ex,
	},
	feet = {
		item = eq.feet.w_h_furnace,
	},
	finger1 = {
		item = eq.finger.w_kara_blue,
	},
	finger2 = {
		item = eq.finger.w_h_mana,
	},
	trinket1 = {
		item = eq.trinket.slab_def,
	},
	trinket2 = {
		item = eq.trinket.w_def30,
	},
}

eq.feet.enchant = enchants.feet.stam12
eq.offhand.enchant = enchants.offhand.stam18
eq.chest.enchant = enchants.chest.h150
eq.hands.enchant = enchants.hands.armor240
eq.back.enchant = enchants.back.agi12
--~ eq.hands.enchant = enchants.hands.stam10

local b = {
	buffs.mark_of_the_wild,
	buffs.fortitude,
	buffs.blood_pact,
--~ 	buffs.arcane_brilliance,
--~ 	buffs.spirit,
	buffs.well_fed_stamspirit,
--~ 	buffs.winterveil_spirit,
	buffs.blessing_kings,
--~ 	buffs.wisdom_elixir,
}

utils.InsertAuras(warrior, utils.GetEquipmentAuras(eq))
utils.InsertAuras(warrior, b)

function print_warrior_info(v)
	local delta_def = math.floor(v.skill_defense) - 350
	local avoidance = 5 + (delta_def < 0 and 0 or delta_def) * 0.04 + v.dodge + v.parry
	local armor_mitigation = utils.GetArmorMitigation(v.armor, 70)
	local mitigation = avoidance + (100 - avoidance) * armor_mitigation
	local ehealth = v.maxhealth * (1 / (1 - mitigation / 100))
	print(
		("Physical avoidance: %.2f%%"):format(avoidance),
		("Armor absorption: %.2f%%"):format(armor_mitigation * 100),
		("Physical mitigation: %.2f%%"):format(mitigation),
		("Effective health: %d HP"):format(ehealth),
		("Block : %.2f%% / %d"):format(v.block, v.blockvalue)
	)
	
	for _, data in pairs{
		{ 1, 1000 },	{ 1.5, 1000 },
		{ 5, 1000 },	{ 1.5, 2000 },
		{ 1, 5000 },	{ 2, 5000 },
	} do
		local speed, dps = data[1], data[2]
		local idps = dps * armor_mitigation
		local hit = idps * speed
		local block_mitigation = (hit - v.blockvalue) / hit
		local fully_taken = 100 - avoidance - v.block
		fully_taken = fully_taken < 0 and 0 or fully_taken
		local taken_dps = fully_taken / 100 * idps + v.block / 100 * (idps * block_mitigation)
		local ttl = v.maxhealth / taken_dps
		print(("Mob hits:  %d@%.1f equals %.1f IDPS (%.2f%%) TTL : %.1fs"):format(dps, speed, taken_dps, taken_dps / dps * 100, ttl))
	end
end

function main()

	local t1 = tier1.calc(warrior)

--~ 	print("----")
--~ 	for k, v in pairs(t1.ratings) do
--~ 		print(k, v, ("%.2f"):format(utils.GetRatingBonus(k, v, 70)))
--~ 	end

	local v = tier2.calc(warrior)

--~ 	for s, v in pairs(v) do
--~ 		if v ~= 0 then print(s, v) end
--~ 	end

--~ 	print("----")
	print_warrior_info(v)
	
--~ 	print_warrior_info({
--~ 		skill_defense = 536,
--~ 		dodge = 27.06,
--~ 		parry = 17.44,
--~ 		armor = 16830,
--~ 		maxhealth = 15424,
--~ 	})
--~ 	
--~ 	print_warrior_info({
--~ 		skill_defense = 498,
--~ 		dodge = 14.95,
--~ 		parry = 16.05,
--~ 		armor = 12478,
--~ 		maxhealth = 11424,
--~ 	})
--~ 	
--~ 	print_warrior_info({
--~ 		skill_defense = 476,
--~ 		dodge = 11.39,
--~ 		parry = 15.8,
--~ 		armor = 10945,
--~ 		maxhealth = 10851,
--~ 	})
	
end

main()
